<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>扫雷扫雷</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body,html{
            width: 100%;
            height: 100%;
            background-color: black;
        }
        .box{
            width: 624px;
            height: 624px;
            border: 1px solid white;
            margin: 50px auto;
        }
        .box i{
            font-style: normal;
            display: block;
            width: 50px;
            height: 50px;
            border: 1px solid white;
            float: left;
            font-size: 30px;
            text-align: center;
            line-height: 50px;
        }

    </style>
</head>
<body>
    <!-- 12 x 12, 20个雷 -->


    <div class="box" id="box"></div>

    <script>
        // 加载界面小方块
        var oBox = document.getElementById("box");
        var oI = document.getElementsByTagName("i");
        var eleText = "";
        for(var i = 0; i < 12; i++){
            for(var j = 0; j < 12; j++){
                eleText += "<i></i>"
            }
        }

        oBox.innerHTML = eleText;

        for(var i = 0; i < 144; i++){
            oI[i].lei = 1;
            oI[i].in = -1;
            oI[i].sub = i;
        }

        for(var i = 0; i < 20; i++){
            lei();
        }


        oBox.onclick = function(eve){
            var ev = window.event || eve;
            var tEle = ev.target;
            var t = ev.target.sub;
            if(tEle.in == -1){
                tEle.in = 0;
                if(tEle.lei == 0){
                    tEle.innerHTML = "雷";
                    tEle.style.backgroundColor = "white"
                    tEle.style.border = "1px solid black";
                    alert("失败，这是雷");
                    location.reload();
                }else{
                    pan(t,tEle);
                }
            }
            var tf = true;
            for(var i = 0; i < 144; i++){
                if(oI[i].lei == 1 && oI[i].in < 0){
                    tf = false
                }
            }
            if(tf){
                alert("你成功了")
            }
        }

        // 加载随机数，记录雷的位置函数
        function lei(){
            var rand = Math.floor(Math.random() * oI.length);
            if(oI[rand].lei == 0){
                lei();
            }else{
                oI[rand].lei = 0;
            }
        
            
        }

        // 判断确定数字，返回数字
        function pan(num,ele){
            var up = num - 12;
            var down = num + 12;
            panZhi(up,ele);
            panZhi(down,ele);
            if(num % 12 != 0){
                if(oI[num - 1].lei == 0){
                    ele.in++;
                }
                panZhi(num + 11,ele);
                panZhi(num - 13,ele);
            }
            if((num + 1) % 12 != 0){
                if(oI[num + 1].lei == 0){
                    ele.in++;
                }
                panZhi(num + 13,ele);
                panZhi(num - 11,ele);
            }
            ele.innerHTML = ele.in;
            ele.style.backgroundColor = "white"
            ele.style.border = "1px solid black";
        }


        // 判断上下的值是否有
        function panZhi(num,ele){
            if(num >= 0 && num <144){
                    if(oI[num].lei == 0){
                        ele.in++;
                    }
            }
        }

    </script>
</body>
</html>